// 导入数据库API
const db = require('../../config/db');
// 操作数据库的业务
module.exports = {
    // 示例：
    test() {
        return new Promise((resolve, reject) => { })
    },
    // 发布题目
    add(user_name, topic_id, topic_type, topic_content, topic_analysis, exam_time, exam_type, topic_lib_id, user_id,topic_options,topic_val,topic_answer,topic_lib_name) {
        // console.log({user_name, topic_id, topic_type, topic_content, topic_analysis, exam_time, exam_type, topic_lib_id, user_id,topic_options,topic_val,topic_answer,topic_lib_name})
        return new Promise((resolve, reject) => {
            let sql = `insert into db_topic(user_name,topic_id,topic_type,topic_content,topic_analysis,exam_time,exam_type,topic_lib_id,user_id,topic_options,topic_val,topic_answer,topic_lib_name) values('${user_name}','${topic_id}','${topic_type}','${topic_content}','${topic_analysis}','${exam_time}','${exam_type}','${topic_lib_id}','${user_id}','${topic_options}','${topic_val}','${topic_answer}','${topic_lib_name}')`;
            db.query(sql, (err, rows) => {
                console.log(err);
                if (err) {
                    reject({
                        code: -1,
                        msg: '发布题目失败。'
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: '发布题目成功。'
                    })
                }
            })
        })
    },

    // 删除题目（用户自己删除）
    remove(topic_id, user_id) {
        return new Promise((resolve, reject) => {
            let sql = `delete from  db_topic where topic_id = "${topic_id}" and user_id="${user_id}"`
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: '删除题目失败。'
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: '删除题目成功。'
                    })
                }
            });
        })
    },

    // 删除题目（管理员删除）
    delete(topic_id) {
        return new Promise((resolve, reject) => {
            let sql = `delete from  db_topic where topic_id = "${topic_id}"`
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: '删除题目失败。'
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: '删除题目成功。'
                    })
                }
            });
        })
    },

    // 题目列表(全部)
    list2(page_num, page_size) {
        return new Promise((resolve, reject) => {
            let startIndex = (page_num - 1) * page_size;
            let len = page_size;
            let sql = "select * from db_topic limit " + startIndex + "," + len + "";
            db.query(sql, (err, rows) => {
                if (err) {
                    resolve({
                        code: -1,
                        msg: '获取题目列表失败。',
                        err
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: "获取题目列表成功。",
                        result: rows,
                        page: {
                            page_num: page_num,
                            page_size: page_size,
                        }
                    })
                }
            })
        })

    },

    // 题目列表(我的)
    list(page_num, page_size,user_id) {
        return new Promise((resolve, reject) => {
            let startIndex = (page_num - 1) * page_size;
            let len = page_size;
            let sql = `select * from db_topic where user_id = "${user_id}" limit  ${startIndex}, ${len}`;
            db.query(sql, (err, rows) => {
                if (err) {
                    resolve({
                        code: -1,
                        msg: '获取题目列表失败。',
                        err
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: "获取题目列表成功。",
                        result: rows,
                        page: {
                            page_num: page_num,
                            page_size: page_size,
                        }
                    })
                }
            })
        })

    },

    // 题目详情
    detail(topic_lib_id) {
        return new Promise((resolve, reject) => {
            let sql = `select * from db_topic where topic_lib_id="${topic_lib_id}"`;
            db.query(sql, (err, rows) => {
                if (err) {
                    resolve({
                        code: -1,
                        msg: '查询题目列表失败。',
                        err
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: "查询题目详情成功。",
                        result: rows
                    })
                }
            })
        })
    },

    // 搜索题目
    // 参数：keyword
    search(keyword) {
        return new Promise((resolve, reject) => {
            let sql = "select * from db_topic where user_name LIKE CONCAT('%" + keyword + "%') or exam_type LIKE CONCAT('%" + keyword + "%') or topic_lib_name LIKE CONCAT('%" + keyword + "%')";
            db.query(sql, (err, rows) => {
                if (err) {
                    resolve({
                        code: -1,
                        msg: '查询题目信息失败。',
                        err
                    })
                } else {
                    if (rows.length == 0) {
                        resolve({
                            code: -1,
                            msg: '没有该关键字匹配题目信息。'
                        })
                    } else {
                        resolve({
                            code: 200,
                            msg: '查询题目信息成功。',
                            result: rows
                        })
                    }
                }
            })
        })
    },

    // 统计题目数量
    total(user_id) {
        return new Promise((resolve, reject) => {
            db.query('select count(*) as total from db_topic where user_id = "'+user_id+'"', (err, rows) => {
                if (err) {
                    resolve({
                        code: -1,
                        msg: '统计题目数量失败。',
                        err
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: "获取题目数量成功。",
                        total: rows[0]['total']
                    })
                }
            })

        })
    },

    total2(user_id) {
        return new Promise((resolve, reject) => {
            db.query('select topic_id from db_topic where user_id = "'+user_id+'"', (err, rows) => {
                if (err) {
                    resolve({
                        code: -1,
                        msg: '统计题目数量失败。',
                        err
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: "获取题目数量成功。",
                        total: rows[0]['total']
                    })
                }
            })

        })
    },

    total3() {
        return new Promise((resolve, reject) => {
            db.query('select count(*) as total from db_topic', (err, rows) => {
                if (err) {
                    resolve({
                        code: -1,
                        msg: '统计题目数量失败。',
                        err
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: "获取题目数量成功。",
                        total: rows[0]['total']
                    })
                }
            })

        })
    },
    // 获取题库中指定类型的题目数量
    topic_type_total(topic_lib_id, topic_type){
        return new Promise((resolve, reject) => {
            let sql = `select count(*) as total from db_topic where topic_lib_id = "${topic_lib_id}" and topic_type="${topic_type}"`
            db.query(sql, (err, rows) => {
                if (err) {
                    resolve({
                        code: -1,
                        msg: '统计题目数量失败。',
                        err
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: "获取题目数量成功。",
                        total: rows[0]['total']
                    })
                }
            })

        })
    },

}